package org.android.smsbombing;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 
 * @author azalsup
 *
 */
public class smsbombing extends Activity implements Button.OnClickListener{
	
	private Button boutonenvoie;
	private EditText numeroTelephone;
	private EditText message;
	private boolean envoyer = false;
	private int nbSMSEnvoyees = 0;
	
	private String strNumeroPhone = "";
	private String strMsgmessage = "";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        this.boutonenvoie = (Button) findViewById(R.id.SEND_BUTTON);
        this.numeroTelephone = (EditText) findViewById(R.id.PHONE_NUMBER_1);
        this.message = (EditText) findViewById(R.id.MESSAGE_1);
 
      //listeners
        this.boutonenvoie.setOnClickListener(this);
        
        this.procedure();
        
    }

	@Override
	public void onClick(View v) {
		this.strNumeroPhone = this.numeroTelephone.getText().toString();
		this.strMsgmessage = this.message.getText().toString();
	
		if ((! this.envoyer ) && ( this.strNumeroPhone.length() > 0 ) && ( this.strMsgmessage.length() > 0 )){
			this.envoyer = true;
			 
		}
		else{
			if (! this.envoyer){
				Toast.makeText(getBaseContext(), 
		                "Please enter both phone number and message.", 
		                Toast.LENGTH_SHORT).show();
			}
		
			this.envoyer = false;
			
		}
		
		if (this.envoyer){
			this.boutonenvoie.setText("Stop Sending");
		}
		else{
			this.boutonenvoie.setText("Start Sending");
		}
		
	} 
	

	public void procedure() {


		while (this.envoyer){
			this.envoyerSMS();
			this.nbSMSEnvoyees += 1;
			
			try {
				Thread.sleep(4000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		
	} 
	

    private void envoyerSMS()
    {        
        PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, smsbombing.class), 0);                
        SmsManager shortMessage = SmsManager.getDefault();
        shortMessage.sendTextMessage(this.strNumeroPhone, null, this.strMsgmessage, intent, null);        
    }   

}